package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.Manager;
import edu.its.dreamteam.daf.dao.IManagerDao;

public class UserGrid {
	@Inject
	private IManagerDao managerDao;
	private List<Manager> managerList;
	private Manager manager;	
	
	@Retain
	private BeanModel managerModel;
	@Inject
	private BeanModelSource beanModelSource;
    @Inject
    private ComponentResources resources;
    
    private boolean delete;
    private List<String> deletedList;
    
    @Component
    private Form managerForm;
    
	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public IManagerDao getManagerDao() {
		return managerDao;
	}

	public boolean isDelete() {
		return delete;
	}    
	
	public void setDelete(boolean delete) {
		if(delete){			
			getDeletedList().add(getManager().getUsername());
		}
		
	}	
	public List<String> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<String>();				
		}
		return deletedList;
	}
	
	public List<Manager> getManagerList() {
		if(managerList == null){
			managerList = new ArrayList<Manager>();	
			managerList = getManagerDao().queryForList("", "");
		}
		
		return managerList;
	}
	
	public BeanModel getManagerModel() { 
		return managerModel; 
	}
	
	void pageLoaded(){
    	managerModel = beanModelSource.create(Manager.class, false, resources);    	
    	managerModel.add("actions", null).label("Edit");
    	managerModel.reorder("actions");
    }	
	
	Object onSuccessFromManagerForm(){
		for (String element : getDeletedList()) {
			setManager((Manager) getManagerDao().findByPrimaryKey(element));
			getManagerDao().delete(getManager());
									
		}	
		deletedList = null;
		return Manager.class;
	}
	
	
}
